package com.wudl.flink;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : Utils
 * @Description :
 * @Author :wudl
 * @Date: 2021-03-14 00:39
 */

public class Utils {
    Map<String, Object> mapData = new HashMap<>();

    public static String opreator(String json) throws Exception {
        String sql = null;
        JSONObject jsonObject = JSONObject.parseObject(json);
        String opreatorType = (String) jsonObject.get("type");
        String tableName = (String) jsonObject.get("table");
        String databaseName = (String) jsonObject.get("database");
        String struct = jsonObject.get("date").toString();
        Map mapTypes = JSON.parseObject(struct);
        for (Object obj : mapTypes.keySet()) {
            if (obj.toString().contains("time"))
            {
//                System.out.println("key为：" + obj + "值为：" + mapTypes.get(obj));
//                String dateFormat = DateUtils.dateFormat(mapTypes.get(obj).toString());
//                mapTypes.put(obj,dateFormat);
            }

        }
        System.out.println("---" + struct);
        if ("create".equals(opreatorType)) {
            sql = CreateSqlUtil.getSql(tableName, "insert", mapTypes, false, "");
        } else if ("delete".equals(opreatorType)) {
            sql = CreateSqlUtil.getSql(tableName, opreatorType, mapTypes, false, "");
        } else if ("update".equals(opreatorType)) {
            sql = CreateSqlUtil.getSql(tableName, opreatorType, mapTypes, false, "");
        } else {
            sql = CreateSqlUtil.getSql(tableName, opreatorType, mapTypes, false, "");
        }
        System.out.println("sql***"+sql);
        return sql;
    }

    public static void main(String[] args) throws Exception {
//        String s = "{\"date\":{\"activity_name\":\"小黄\",\"start_time\":1615681398000,\"create_time\":1615681402000,\"activity_type\":\"2021\",\"activity_desc\":\"小姐姐\",\"end_time\":1615681400000,\"id\":7},\"database\":\"gmall\",\"type\":\"create\",\"table\":\"activity_info\"}";
//        String s = "{\"date\":{\"create_time\":\"2021-03-15 10:05:02\",\"name\":\"hdfs\",\"id\":1,\"age\":28},\"database\":\"test\",\"type\":\"delete\",\"table\":\"test02\"}";
        String s = "{\"date\":{\"create_time\":\"2021-03-15T10:05:05Z\",\"name\":\"hive\",\"id\":2,\"age\":20},\"database\":\"test\",\"type\":\"create\",\"table\":\"test02\"}";
        Utils.opreator(s);
    }


}
